package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.DbException;
/**
 * 数据访问抽象类
 * @author k88
 *
 */
public abstract class BaseDao {
	/**
	 * 将结果集转换为对象Object
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	protected abstract Object handle(ResultSet rs)throws SQLException;
    /**
     * count统计
     * @param sql
     * @return
     * @throws DbException
     */
	public int count(String sql)throws DbException{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs =null;
		int count =0;
		try{
			conn =DbUtil.getConn(); 
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			if(rs!=null&&rs.next()){
				 count=rs.getInt(1);
			}
			return count;
		}catch (SQLException e) {
			throw new DbException("执行count SQL失败", e);
		}
		
	}
}
